<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>商品展示初级版本</title>
    <script src="../js/vue.js"></script>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
  </head>
  <body>
    <div id="app">
      <div class="container">
      <h2 class="text-center">商品展示</h2>
        <product-list :products="products"></product-list>
      </div>
    </div>

    <!-- 定义商品项组件模板 -->
    <template id="product-item-template">
      <div class="col-md-4">
        <div class="card">
          <div class="card-body">
            <h4 class="card-title">{{ product.name }}</h4>
            <p class="card-text">价格：￥{{ product.price }}</p>
            <p class="card-text">{{ product.description }}</p>
          </div>
        </div>
      </div>
    </template>

    <!-- 定义商品列表组件模板 -->
    <template id="product-list-template">
        <!-- 使用商品项 -->
      <div class="row">
        <product-item
          v-for="product in products"
          :key="product.name"
          :product="product"
        ></product-item>
      </div>
    </template>

    <script type="text/javascript">
      //定义商品项组件
      Vue.component("product-item", {
        //... 选项...
        props: ["product"],
        template: "#product-item-template",
      });

      //定义商品列表组件
      Vue.component("product-list", {
        //... 选项...
        props: ["products"],
        template: "#product-list-template",
      });

      //创建一个根实例
      new Vue({
        el: "#app",
        data: {
          // 商品列表数据
          products: [
            {
              name: "iPhone 15",
              price: 999,
              description: "最新款苹果手机，性能强劲",
            },
            {
              name: "MacBook Pro",
              price: 1999,
              description: "高性能笔记本电脑，适合专业人士",
            },
            {
              name: "Apple Watch",
              price: 399,
              description: "智能手表，具备多种健康监测功能",
            },
          ],
        },
      });
    </script>
  </body>
</html>
